//
//  XmlDataSheetCPP.cpp
//  test_xml
//
//  Created by bean on 13/1/4.
//
//

#include "XmlDataSheetCPP.h"
#import "XML_Loader.h"
#import "DataSheet.h"
XmlDataSheetCPP::XmlDataSheetCPP(const char *sheetName)
{
//    sheetDict = [XML_Loader LoaderXML_WithName:@"game_data"];
    NSString *name = [[NSString alloc] initWithUTF8String:sheetName];
    sheetDict = [XML_Loader LoaderXML_WithName:name];
    
}
int XmlDataSheetCPP::getSheetCount()
{
    NSDictionary *sheets = (NSDictionary*)sheetDict;
    return [sheets count];
}
void XmlDataSheetCPP::printAllSheetName()
{
    NSArray* sheets = [(NSDictionary*)sheetDict allValues];
    DataSheet* ds;
    NSLog(@"count = %d", [sheets count]);
    for (int i = 0; i < [sheets count]; i++) {
        ds = [sheets objectAtIndex:i];
        NSLog(@"i = %d, %@",i, [ds getSheetName]);
        for (int j = 0; j < this->getColumnCount((char*)"item"); j++) {
            printf("%s", this->getColumnName((char*)"item", j));
            for (int k = 0; k < this->getRowCount((char*)"item"); k++) {
                printf(" %s ", this->getData((char*)"item", j, k));
            }
            printf("\n");
        }
    }
}
int XmlDataSheetCPP::getColumnCount(const char *sheetName)
{
    if (sheetName == NULL) {
        return NULL;
    }
    NSString *name = [[NSString alloc] initWithUTF8String:sheetName];
    
    
    DataSheet* ds = [(NSDictionary*)sheetDict objectForKey:name];
    
    return [ds getColumnCount];
}

const char* XmlDataSheetCPP::getColumnName(const char *sheetName, int col)
{
    if (sheetName == NULL) {
        return NULL;
    }
    NSString *name = [[NSString alloc] initWithUTF8String:sheetName];

    
    DataSheet* ds = [(NSDictionary*)sheetDict objectForKey:name];
    
    if (ds == NULL) {
        return NULL;
    }
    
    
    return [[ds getColumnWithIndex:col] cStringUsingEncoding:NSASCIIStringEncoding];
}
const char* XmlDataSheetCPP::getData(const char *sheetName, const char *colName, int row)
{
    if (sheetName == NULL || colName == NULL) {
        return NULL;
    }
    NSString *sheetName_obj = [[NSString alloc] initWithUTF8String:sheetName];
    NSString *colName_obj = [[NSString alloc] initWithUTF8String:colName];
    
    DataSheet* ds = [(NSDictionary*)sheetDict objectForKey:sheetName_obj];
    
    if (ds == NULL) {
        return NULL;
    }
    
    
    return [[ds getRowDataByColumnNameWithRowIndex:row columnName:colName_obj] cStringUsingEncoding:NSASCIIStringEncoding];
}
const char* XmlDataSheetCPP::getData(const char *sheetName, int col, int row)
{
    if (sheetName == NULL) {
        return NULL;
    }
    NSString *sheetName_obj = [[NSString alloc] initWithUTF8String:sheetName];
    
    DataSheet* ds = [(NSDictionary*)sheetDict objectForKey:sheetName_obj];
    
    if (ds == NULL) {
        return NULL;
    }
    
    
    return [[ds getRowDataByColumnIndexWithRowIndex:row columnIndex:col] cStringUsingEncoding:NSASCIIStringEncoding];
}
int XmlDataSheetCPP::getRowCount(const char *sheetName)
{
    if (sheetName == NULL) {
        return NULL;
    }
    NSString *sheetName_obj = [[NSString alloc] initWithUTF8String:sheetName];
    
    DataSheet* ds = [(NSDictionary*)sheetDict objectForKey:sheetName_obj];
    
    if (ds == NULL) {
        return NULL;
    }
    
    
    return [ds getRowCount];
}